<template>
  <div class="app-container">
    <div class="filter-container">
      <span style="float:left;width:30%;">
        <treeselect :options="addrTree" v-model="addrId" @input="selectAddr" :normalizer="normalizer"
          :searchable="true" :show-count="true" placeholder="请选择行政分区" style="width:100%;" />
      </span>
      <span style="float:left;margin-left: 10px;">
        <el-button @click="handleFilter" class="filter-item" type="primary" v-waves icon="el-icon-search">查询</el-button>
        <el-button @click="handleCreate" v-if="addr_btn_add" :disabled="btnDisabled"
               class="filter-item" type="primary" v-waves icon="el-icon-plus">添加
        </el-button>
      </span>
      <span style="float:left;margin-left: 10px;">
        <export-excel :btnDisabled="btnDisabled" :restPath="restPath" :list="list" />
      </span>
    </div>
    <!-- 表格 -->
    <el-table :data="list" v-loading="listLoading" border fit highlight-current-row style="width: 100%;height: 100%;">
      <el-table-column width="60px" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 + (listQuery.pageSize*(listQuery.page-1))}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="adCd" label="行政区划代码" width="200px" align="center" />
      <el-table-column prop="adNm" label="行政区划名称" align="center" />
      <el-table-column prop="adShnm" label="行政区划简称" width="200px" align="center" />
      <el-table-column prop="level" label="层级" width="100px" align="center" />
      <el-table-column label="状态" width="80px" align="center">
        <template slot-scope="scope">
          <span>{{getNameByCode(scope.row.status, allDictData.statusOps)}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button v-if="addr_btn_edit" @click="handleUpdate(scope.row.id)" size="small" type="success">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>


    <!-- 添加修改弹出框页面组件 -->
    <add-update v-if="showAdd" @flushList="flushList" @closeDialog="closeDialog" :restPath="restPath" :showAdd="showAdd" :btnType="btnType" :rowId="rowId" :allDictData="allDictData" />

  </div>
</template>

<script>
  /* mixins */
  import pageListMixin from '@/mixins/pageList/pageListMixin.js'
  
  import {
    getAddrTree,
    loadFirstAddrTree,
    getSonAddrTree
  } from '@/api/sys/addr'
  import { getDictData } from '@/api/common'
  
  /* 组件 */
  import addUpdate from './addUpdate.vue'
  
  import * as common from '@/utils/base/common'
  import { getNameByCode } from '@/utils/dictData'

  export default {
    /*  内容: 行政分区  */
    name: 'addr',
    data() {
      return {
        restPath: 'sys/addr',
        listQuery: {
          page: 1,
          pageSize: 10,
          pid: '-1',
        },
        addr_btn_edit: true,
        addr_btn_del: true,
        addr_btn_add: true,
        
        /* 行政分区 */
        addrId: null,
        addrTree: [],
        normalizer(node) {
          if (node.children && !node.children.length) {
            delete node.children
          }
          return {
            id: node.id,
            label: node.adNm,
            children: node.children
          }
        },
        
        allDictData: {
          statusOps: []
        },
        
      }
    },
    mixins: [pageListMixin],
    components: {
      'add-update': addUpdate,
    },
    methods: {
      initData() {
        //              this.addr_btn_edit = this.elements['addr:btn_edit']
        //              this.addr_btn_del = this.elements['addr:btn_del']
        //              this.addr_btn_add = this.elements['addr:btn_add']
        this.getAllDictData()
        this.getAddrTree()
        this.getList()
      },
      
      getAllDictData(){
        const params = {
          dictIds: "STATUS"
        }
        getDictData(params).then(response => {
          let data = response.data
          this.allDictData.statusOps = data.STATUS
        }).catch(err => {
          common.message(this, 'error', 2, '获取数据字典失败!')
          console.error(err)
        })
      },
      
      getNameByCode(val, dictMap){
        return getNameByCode(val, dictMap)
      },
      
      getAddrTree() {
        getAddrTree().then(response => {
          this.addrTree = response.data
        }).catch(err => {
          common.message(this, 'error', 2, '删除数据失败!')
          console.error(err)
        })
      },
      
      selectAddr(){
        this.listQuery.pid = this.addrId
        this.getList()
      },

    } // methods end
  }
</script>
<style scoped>
  @import "~@/styles/list.css";
</style>